package reflect.annotations;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个类名:");
        String className = sc.nextLine();
        Class cls = Class.forName(className);
        Constructor constructor = cls.getConstructor();
        Object obj = constructor.newInstance();
        if(cls.isAnnotationPresent(AutoRunClass.class)){
            Method[] methods =cls.getDeclaredMethods();
            for (Method method : methods){

                if(method.isAnnotationPresent(AutoRunMethod.class)){
                    AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                    int num = arm.value();
                    if(method.getModifiers()== Modifier.PRIVATE){
                        method.setAccessible(true);
                        method.invoke(obj);
                        method.setAccessible(false);
                    }else{
                        for (int i=0;i<num;i++) {
                            method.invoke(obj);
                        }
                    }

                }
            }
        }else{
            System.out.println("该类没有被@AutoRunClass注解修饰");
        }
    }
}
